package com.zking.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zking.entity.Order;
import com.zking.mapper.IOrderMapper;
import com.zking.service.IOrderService;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;
import java.util.Map;

// 实现了Mybatis-Plus的IService接口，实现类只要继承ServiceImpl<对应Mapper, 对应实体类>即可！
@Service
public class OrderServiceImpl extends ServiceImpl<IOrderMapper, Order> implements IOrderService {
    // private final IOrderMapper orderMapper;
    
    @Override
    public Page<Order> search(String name,
                              String address,
                              Double min, Double max,
                              Timestamp date1, Date date2,
                              int pageNum, int pageSize) {
        IOrderMapper mapper = getBaseMapper();
        /* mapper.selectPage(Page.of(pageNum, pageSize), new QueryWrapper<Order>()
                .eq("info", name)
                .like("address", address)
                .ge("price", min)
                .le("price", max)
                .gt("update_date", date1)); */
        
        QueryWrapper<Order> wrapper = new QueryWrapper<>();
        if (name != null && ! name.isEmpty()) {
            wrapper.eq("info", name);
        }
        if (address != null && ! address.isEmpty()) {
            wrapper.like("address", address);
        }
        if (min != null && min >= 0) {
            wrapper.ge("price", min);
        }
        if (max != null && max >= 0) {
            wrapper.le("price", max);
        }
        if (date1 != null) {
            wrapper.gt("update_date", date1);
        }
        // wrapper.last("where username like xxx")
        return mapper.selectPage(Page.of(pageNum, pageSize), wrapper);
    }
    
    @Override
    public Page<Order> search(int pageNum, int pageSize, Map<String, Object> params) {
        QueryWrapper<Order> wrapper = new QueryWrapper<>();
        String name = (String) params.get("name");
        String address = (String) params.get("address");
        Double min = (Double) params.get("min");
        Double max = (Double) params.get("max");
        Timestamp date1 = (Timestamp) params.get("date1");
        Date date2 = (Date) params.get("date2");
        
        wrapper.eq(name != null && ! name.isEmpty(), "info", name)
                .like(address != null && ! address.isEmpty(), "address", address)
                .ge(min != null && min >= 0, "price", min)
                .le(max != null && max >= 0, "price", max)
                .gt(date1 != null, "update_date", date1);
        return getBaseMapper().selectPage(Page.of(pageNum, pageSize), wrapper);
    }
}
